package top.hg.tool.modules.joke;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: skh
 * @Date: 2019/7/12 14:20
 * @Description:
 */
@Service
@Slf4j
public class JokeService {

	private String url = "https://m2.qiushibaike.com/article/list/text?count=30&page=1";

	public void getJokeList() {
		String res = HttpUtil.get(url);
		System.out.println("res = " + res);
	}

	public static void saveJoke(JokeDO jokeDO) {
		log.info("保存成功");

	}

//	public static void main(String[] args) {
//		Integer sleepTime = 1000;
//
//		for (int i = 1; i <= 10; i++) {
//			String url = "https://m2.qiushibaike.com/wx/video/list?count=6&page=" + i;
//			HttpRequest httpRequest = new HttpRequest(url);
//			httpRequest = httpRequest.header("Uuid", "ios_0d6ac286ba8e466f84800e781b112150").contentType("application/json");
//			httpRequest = httpRequest.header("qbaid", "B7810816-9E94-4A21-AA4C-6386438E93CF");
//			httpRequest = httpRequest.header("User-Agent", "QiuBai/11.9.1 rv:1");
//			String body = httpRequest.execute().body();
//			JokeEntity jokeEntity = JSONUtil.toBean(body, JokeEntity.class);
//			if (jokeEntity.getErr() != 0) {
//				log.error("请求url:{},发生错误", url);
//				try {
//					Thread.sleep(sleepTime);
//				} catch (InterruptedException e) {
//					e.printStackTrace();
//				}
//				continue;
//			}
//
//			List<JokeEntity.ItemsBean> items = jokeEntity.getItems();
//			if (CollUtil.isEmpty(items)) {
//				log.error("请求url:{},获取列表为空", url);
//				try {
//					Thread.sleep(sleepTime);
//				} catch (InterruptedException e) {
//					e.printStackTrace();
//				}
//				continue;
//			}
//
//			int invaildCount = 0;
//			int vaildCount = 0;
//			int totalCount = items.size();
//			for (JokeEntity.ItemsBean item : items) {
//
//				if (item.getVotes() == null || item.getVotes().getUp() < 1000) {
//					invaildCount++;
//					continue;
//				}
//
//				JokeDO jokeDO = new JokeDO();
//				jokeDO.setQsbkId(item.getId());
//				jokeDO.setFormat(item.getFormat());
//				jokeDO.setImage(item.getImage());
//				jokeDO.setTag(item.getTag());
//				jokeDO.setContent(item.getContent());
//				jokeDO.setState(item.getState());
//				jokeDO.setType(item.getType());
//				jokeDO.setUp(item.getVotes().getUp());
//				jokeDO.setDown(item.getVotes().getDown());
//				jokeDO.setCommentsCount(item.getComments_count());
//				jokeDO.setShareCount(item.getShare_count());
//				jokeDO.setCreateTime(new Date(item.getCreated_at()));
//				jokeDO.setPublishedTime(new Date(item.getPublished_at()));
//				if (item.getUser() != null) {
//					jokeDO.setMedium(item.getUser().getMedium());
//					jokeDO.setThumb(item.getUser().getThumb());
//					jokeDO.setLogin(item.getUser().getLogin());
//					jokeDO.setIcon(item.getUser().getIcon());
//					jokeDO.setQsbkUid(item.getUser().getUid());
//				}
//
//				saveJoke(jokeDO);
//				vaildCount++;
//			}
//
//			log.info("请求页数:{},总数:{},有效数量:{},无效数量:{}", i, totalCount, vaildCount, invaildCount);
//			try {
//				Thread.sleep(sleepTime);
//			} catch (InterruptedException e) {
//				e.printStackTrace();
//			}
//
//
//		}
//	}

	public List<VideoEntity.ItemsBean> getVideoList(int page) {
		String url = "https://m2.qiushibaike.com/wx/video/list?count=6&page=" + page;
		HttpRequest httpRequest = new HttpRequest(url);
		httpRequest = httpRequest.header("Uuid", "ios_0d6ac286ba8e466f84800e781b112150").contentType("application/json");
		httpRequest = httpRequest.header("qbaid", "B7810816-9E94-4A21-AA4C-6386438E93CF");
		httpRequest = httpRequest.header("User-Agent", "QiuBai/11.9.1 rv:1");
		String body = httpRequest.execute().body();
//		System.out.println("body = " + body);
		VideoEntity videoEntity = JSONUtil.toBean(body, VideoEntity.class);
		List<VideoEntity.ItemsBean> items = videoEntity.getItems();
		return items;
	}

	public static void main(String[] args) {
		JokeService jokeService = new JokeService();
		List<String> stringList = new ArrayList<>();
		for (int i = 1; i < 10; i++) {
			List<VideoEntity.ItemsBean> videoList = jokeService.getVideoList(i);
			for (VideoEntity.ItemsBean itemsBean : videoList) {
				stringList.add(itemsBean.getContent());
			}
		}
		System.out.println("stringList = " + stringList);

	}
}
